package com.ebelter.nb.model.nb.uploaders;


import android.os.SystemClock;

import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.nb.beans.uploadbeans.WillLoadDataBean;
import com.ebelter.nb.constants.NbConstants;
import com.ebelter.nb.model.http.HttpResponse2;
import com.ebelter.nb.model.http.MyStringCallback2;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.model.http.response.NbBaseResponse;
import com.ebelter.nb.model.jg.jgbean.JGBpmR;
import com.ebelter.nb.model.jg.jgbean.JGScaleR;

import java.util.ArrayList;
import java.util.List;

//数据上传者
public class DataUploader extends BaseHandle{

    private static final String TAG = "DataUploader";
    private List<WillLoadDataBean> uploadList ;

    private static long threadSleepTime = 6000 ;//线程休眠时间 有任务时可以减少一点 没任务时可以增大一点


    private UploadThread upLoadThread;


    public DataUploader() {
        this.uploadList = new ArrayList<>();

    }

    public void starUploadThread(){
        if (upLoadThread!=null&&upLoadThread.isRuningFlag&&upLoadThread.isAlive()){
            //说明正在运行
        }else {
            upLoadThread = new UploadThread();
            upLoadThread.start();
        }
    }




    public void addData(WillLoadDataBean dataBean){
        if (uploadList!=null&&dataBean!=null)uploadList.add(dataBean);
    }



    public class UploadThread extends Thread{

        public boolean isRuningFlag = true;//线程是否正在运行的标志

        @Override
        public void run() {
            while (isRuningFlag){
                if (NetUtils.available()&& uploadList!=null&&uploadList.size()>0){//网络可用 没有正在上传的任务及数据池里面有数据的时候才去上传
                    WillLoadDataBean will = uploadList.remove(0);//取到第一个数据
                    upLoads(will);
                }

                if (uploadList!=null&&uploadList.size()>0){
                    threadSleepTime=5000;
                }else {
                    threadSleepTime=10000;
                }
                SystemClock.sleep(threadSleepTime);
            }
        }
    }

    private void upLoads(WillLoadDataBean willData) {
        if (willData==null)return;
        if (willData.mesureDataTyle==0){
            //血压计
            uploadBpm((JGBpmR.DeviceInfoBean)willData);
        }else if (willData.mesureDataTyle==1){
            //体脂秤
            uploadScale((JGScaleR.DeviceInfoBean)willData);
        }

    }

    //上传体脂秤数据
    private void uploadScale(final JGScaleR.DeviceInfoBean s) {
        NbNetUtils.getInstance().uploadScaleData(s, s.authKey, s.imei, s.time, s.muscleV, s.fat2, (int) s.visceralFat,
                s.water, s.boneV, 0, s.bmr, 0, s.bmi, s.resistor2,s.weight2,s.protein, s.hart2,
                s.skeletalMuscleV,s.bodyAge,s.score,new HttpResponse2<NbBaseResponse>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestResultDesc, int requestResultCode, NbBaseResponse response, String s1) {

                        UserSpUtil.writeFloat(NbConstants.User.weight,s.weight2);
                        LogUtils.i(TAG,"---------上传体脂数据 s = " + s);
                        if (isRequestSuccess&&requestResultCode== MyStringCallback2.SUCCESS){
                            //说明上传成功
                            LogUtils.i(TAG,"---------上传体脂数据成功" + s.toString());
                        }else {
                            //说明上传失败
                            LogUtils.i(TAG,"---------上传体脂数据失败------" + s.toString());
                            s.currentUploadCount++;
                            if (s.currentUploadCount <= s.repeatUploadMaxCount){
                                if (uploadList!=null)uploadList.add(0,s);
                            }else {
                                LogUtils.i(TAG,"------------------------这条体脂数据上传多次失败");
                            }

                        }




                    }
                });
    }


    //上传血压计数据
    private void uploadBpm(final JGBpmR.DeviceInfoBean d) {

        NbNetUtils.getInstance().uploadBpmData(d, d.authKey,d.userType,
                NumUtils.string2Int(d.user),
                NumUtils.string2Int(d.ano), d.imei, d.time,
                NumUtils.string2Int(d.sys),
                NumUtils.string2Int(d.dia),
                NumUtils.string2Int(d.pul), new HttpResponse2<NbBaseResponse>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestResultDesc, int requestResultCode, NbBaseResponse nbBaseResponse, String s) {


                        LogUtils.i(TAG,"---------上传血压数据 s = " + s);
                        if (isRequestSuccess&&requestResultCode== MyStringCallback2.SUCCESS){
                            //说明上传成功
                            LogUtils.i(TAG,"---------上传血压数据成功" + d.toString());
                        }else {
                            //说明上传失败
                            LogUtils.i(TAG,"---------上传血压数据失败------" + d.toString());
                            d.currentUploadCount++;
                            if (d.currentUploadCount <= d.repeatUploadMaxCount){
                                if (uploadList!=null)uploadList.add(0,d);
                            }else {
                                LogUtils.i(TAG,"------------------------这条血压数据上传多次失败");
                            }

                        }

                    }
                });


    }


}
